﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using xiushen.Domain;
using xiushen.Infrastructure;
using xiushen.Models;
using System.Configuration;

namespace xiushen.Controllers
{
    [Authorize]
    public class AdminController : Controller
    {
        private IAdminService service = new AdminRepository();
        //
        // GET: /Admin/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult CreateCategory()
        {
            var categoryis = service.GetAllCategorys(false).Select(r => new SelectListItem { Text = r.Name, Value = r.ID.ToString() }).ToList();
            categoryis.Insert(0, new SelectListItem() { Text = "", Value = null });
            var model = new CatalogModel() {CategoryData = new Category(), Categorys = categoryis};

            return View(model);
        }
        [HttpPost]
        public ActionResult CreateCategory(CatalogModel data)
        {
            service.PersitCatalog(data.CategoryData);
            return View("Index");
        }

        public ActionResult CreateBook()
        {
            var book = service.GetAllCategorys().Select(r => new SelectListItem { Text = r.Name, Value = r.ID.ToString() });
            return View(new UPdateBookBomdel() { BookData = new Book(), Categorys = book });
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult CreateBook(UPdateBookBomdel data)
        {
            service.PersitBook(data.BookData);
            return View("Index");
        }

        public ActionResult FileUpload()
        {
            var book = service.GetAllCategorys(containImage:true).Select(r => new SelectListItem { Text = r.Name, Value = r.ID.ToString() });
            return View(new UploadFileModle() { AttachmentData = new Attachment(), Categorys = book });
        }
        [HttpPost]
        public ActionResult FileUpload(UploadFileModle data)
        {
            var postedFile = data.Picture;
            var destinationFolder = ConfigurationManager.AppSettings["UpLoadFile"];

            if (postedFile.ContentLength > 0)
            {
                string filePath = string.Empty;
                var extension = Path.GetExtension(postedFile.FileName);
                var type = extension == ".pdf" ? Constants.Book : Constants.Image;
                filePath = CreateFolder(data.AttachmentData.CategoryID.ToString(), destinationFolder,type );
                var fileName = String.Format("{0}.{1}", Guid.NewGuid().ToString(), extension);
                filePath = Path.Combine(filePath, fileName);
                data.AttachmentData.FileName = fileName;
                data.AttachmentData.FileType = type;
                postedFile.SaveAs(filePath);
            }
            data.AttachmentData.UploadTime = DateTime.Now;
            data.AttachmentData.UploadUserName = User.Identity.Name;
            service.PersistData(data.AttachmentData);
            return View("Index");
        }
        private string CreateFolder(string categoryId, string folderPath, string type)
        {

            folderPath = Path.Combine(folderPath, type);
            var path = Path.Combine(folderPath, categoryId);
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            return path;
        }
    }
}
